package com.glorysoft.wangnianxinpda.dialog;

import android.app.Dialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatDialogFragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.glorysoft.wangnianxinpda.R;
import com.glorysoft.wangnianxinpda.callback.StringCommonCallBack;
import com.glorysoft.wangnianxinpda.utils.ButtonUtils;
import com.pda.platform.ui.ui_pdaplatform.callback.FreeUI_CommonCallback;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_DisplayUtils;
import com.pda.platform.ui.ui_pdaplatform.utils_public.FreeApi_ToastUtils;
import com.pda.platform.ui.ui_pdaplatform.view.FreeUI_ClearEditText;

/**
 * @Author Zhangjy
 * @Date 2021/6/28 17:52
 * Description :
 */
public class InputFragmentDialog extends AppCompatDialogFragment {
    private String tips = "";
    private String leftStr = "";
    private String rightStr = "";
    private String text = "";
    private View.OnClickListener onClickListener = null;
    private StringCommonCallBack callBack = null;
    private FreeUI_CommonCallback canelBack = null;
    private boolean isPassWord = false;
    private EditText etRemark = null;

    @Override
    public void show(@NonNull FragmentManager manager, @Nullable String tag) {
        if (!isAdded()){
            FragmentTransaction ft = manager.beginTransaction();
            ft.add(this, tag);
            ft.commitAllowingStateLoss();
        }
    }

    @Override
    public void onStart() {
        super.onStart();
        Dialog dialog = getDialog();
        if (dialog != null){
            dialog.getWindow().setLayout(Double.valueOf(FreeApi_DisplayUtils.getScreenWidth() * 0.8).intValue(), ViewGroup.LayoutParams.WRAP_CONTENT);
        }
    }

    public void setCallBackAndWhere(String tips, StringCommonCallBack callBack, String text) {
        this.callBack = callBack;
        this.tips = tips;
        this.text = text;
    }

    public void setCallBackAndWhere(String tips, StringCommonCallBack callBack, String text, Boolean isPassWord) {
        this.callBack = callBack;
        this.tips = tips;
        this.text = text;
        this.isPassWord = isPassWord;
    }

    public void setData(String tips, View.OnClickListener confirmListener) {
        this.tips = tips;
        onClickListener = confirmListener;
    }

    public void setData(String tips, String leftStr, String rightStr, View.OnClickListener confirmListener) {
        this.tips = tips;
        onClickListener = confirmListener;
        this.leftStr = leftStr;
        this.rightStr = rightStr;
    }

    public void setCallBack(FreeUI_CommonCallback canelBack) {
        this.canelBack = canelBack;
    }

    public void clearData() {
        if (etRemark != null) etRemark.setText("");
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.dialog_genaral, null);
        getDialog().getWindow().setGravity(Gravity.CENTER); // 非常重要：设置对话框弹出的位置
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        getDialog().getWindow().setWindowAnimations(R.style.BottomDialog);//添加动画
        getDialog().setCancelable(false);
        return view;
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        Button btnConfirm = view.findViewById(R.id.btnConfirm);//确认按钮
        Button btnCancel = view.findViewById(R.id.btnCancel);//取消按钮
        FreeUI_ClearEditText etRemark = view.findViewById(R.id.etRemark);//dialog内容
        TextView tvTitle = view.findViewById(R.id.tvTitle);

        tvTitle.setText(tips);
        etRemark.setText(text);
        etRemark.setSelection(0, etRemark.getText().length());

        if (leftStr != null && !leftStr.equals("")){
            btnCancel.setText(leftStr);
        }
        if (rightStr != null && !rightStr.equals("")){
            btnConfirm.setText(rightStr);
        }

        btnCancel.setOnClickListener(v -> dismiss());
        btnConfirm.setOnClickListener(v -> {
            if (new ButtonUtils().isFastDoubleClick(v.getId())) {
                FreeApi_ToastUtils.showFreeToast(getString(R.string.doNotClickRepeatedly), getActivity(), true, 0);
                return;
            }
            if (etRemark.getText().toString().trim().equals("")){
                FreeApi_ToastUtils.showFreeToast(getString(R.string.enterTheContent), getActivity(), true, 0);
            }else {
                dismiss();
                callBack.onCallback(etRemark.getText().toString().trim());
            }
        });

        super.onViewCreated(view, savedInstanceState);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (canelBack != null) {
            canelBack.onSuccess();
        }
    }
}
